/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package listener;

/**
 *
 * @author User
 */
import appClient.UserComman;
import com.google.gson.Gson;
import engine.Address;
import engine.City;
import engine.Country;
import engine.FullPersone;
import engine.GType;
import engine.Persone;
import engine.Phone;
import engine.Singleton;
import engine.Street;
import engine.TypeAdress;
import engine.TypePhone;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import protocol.Command;
import server.Player;
import server.ServeOneThread;
import sql.Conector;

public class MyrealListener implements MyListener {

    @Override
  public boolean autorize(MyEvent e)
  {
    try {
      String name = e.data;
      String pass = e.add;
      Statement statement = Conector.conect();
      try {
        String query = "SELECT pass, id, rollid FROM user WHERE LOGIN='" + name + "'";
        ResultSet resultSet = statement.executeQuery(query);
        resultSet.next();
        String passSql = resultSet.getString(1);
        String usId = resultSet.getString(2);
        String rollId = resultSet.getString(3);
        
        query = "SELECT nroll FROM roll WHERE id='" + rollId + "'";
        resultSet = statement.executeQuery(query);
        resultSet.next();
        String roll = resultSet.getString(1);
        
        Player palyer = Singleton.clients.get(Singleton.clients.size()-1);
        Command obj = new Command();
        obj.Type = "Connect";
        if (!passSql.equals(pass))
        {
            obj.Data = "Error";
            Gson gson = new Gson();
            String send = gson.toJson(obj);
            palyer.getSrv().sendUser(send);
           // vis.getCli().refuse("{\"Type\": \"End\"}");
        }
        else{
            
            obj.Data = roll;
            obj.Add = usId;
            Gson gson = new Gson();
            String send = gson.toJson(obj);
            palyer.getSrv().sendUser(send);
            
            palyer.setLogin(name);
            palyer.setId(usId);
            palyer.setRole(roll);
        }
        } catch (Exception ex)
          {
            //  log.info("test" + e);
            Player palyer = Singleton.clients.get(Singleton.clients.size()-1);
            Command obj = new Command();
            obj.Type = "Connect";
            obj.Data = "Error";
            Gson gson = new Gson();
            String send = gson.toJson(obj);
            try {
                palyer.getSrv().sendUser(send);
            }
            catch (Exception exp)
            {
                System.out.println("No Server");
            }
          }
        
        String query = "SHUTDOWN";
        statement.execute(query);
        
    } catch (SQLException ex) {
      Logger.getLogger(MyrealListener.class.getName()).log(Level.SEVERE, null, ex);
    }
    return true;
  }
  
  
  public String setList(MyEvent e)
  {
    ServeOneThread mysrv = e.mysrv;
    String str ="";
    try { 
      Statement statement = Conector.conect();
      
      String query = "SELECT id, fname, lname FROM persone";
      ResultSet resultSet = statement.executeQuery(query);
      while (resultSet.next())
      {
       Persone obj2 = new Persone();
       obj2.ID = resultSet.getString(1);
       obj2.FName = resultSet.getString(2);
       obj2.LNmae = resultSet.getString(3);
       
       Gson gson2 = new Gson();
       str += gson2.toJson(obj2);
       str += ",";
    
      }
      query = "SHUTDOWN";
      statement.execute(query);
      str = str.substring(0, str.length()-1);
           
      str = UserComman.commandGet("List", "[" + str + "]");
      try {
      mysrv.sendUser(str);
       } catch (Exception ex)
        {
            System.out.println("No Server");
          //  log.info("test" + e);
        }
     
    } catch (SQLException ex) {
      Logger.getLogger(MyrealListener.class.getName()).log(Level.SEVERE, null, ex);
    }
    return str;
  }
  
  public String getPersone(MyEvent evn)
  {
    String id = evn.add;
    ServeOneThread mysrv = evn.mysrv;
    String str ="";
    try {
      
      Statement statement = Conector.conect();
      String query = "SELECT id, fname, lname, bd, uid FROM persone WHERE id ='" + id + "'";
      ResultSet resultSet = statement.executeQuery(query);
      resultSet.next();
      
      FullPersone obj2 = new FullPersone();
      obj2.ID = resultSet.getString(1);
      obj2.FName = resultSet.getString(2);
      obj2.LName = resultSet.getString(3);
      obj2.bd = resultSet.getString(4);
      obj2.uid = resultSet.getString(5);
      obj2.Phones = getPhones(id);
      obj2.Addres = getAdddress(id);
      

      Gson gson2 = new Gson();
      str += gson2.toJson(obj2);
      
      query = "SHUTDOWN";
      statement.execute(query);

      
      str = UserComman.commandGet("Person", str);
      try {
      mysrv.sendUser(str);
       } catch (Exception e)
        {
            System.out.println("No Server");
          //  log.info("test" + e);
        }
      System.out.println(str);
    } catch (SQLException ex) {
      Logger.getLogger(MyrealListener.class.getName()).log(Level.SEVERE, null, ex);
    }
    return str;
  }
  
  public boolean sendPhoneT(MyEvent evn)
  {
    ServeOneThread mysrv = evn.mysrv;
    try {
      List<TypePhone> lpt = getPhoneT();
      
      Gson gson = new Gson();
      String send = UserComman.commandGet("PhoneT", gson.toJson(lpt));
      try {
        mysrv.sendUser(send);
       } catch (Exception e)
       {
         System.out.println("No Server");
       }
      
    } catch (SQLException ex) {
      Logger.getLogger(MyrealListener.class.getName()).log(Level.SEVERE, null, ex);
    }
    return true;
  }
  
   public boolean sendAdrT(MyEvent evn) {
    try {
      ServeOneThread mysrv = evn.mysrv;
       List<TypeAdress> lpt = getAdrT();
       
       Gson gson = new Gson();
       String send = UserComman.commandGet("AddrT", gson.toJson(lpt));
       try {
           mysrv.sendUser(send);
       } catch (Exception e)
       {
         System.out.println("No Server");
       }
       
    } catch (SQLException ex) {
      Logger.getLogger(MyrealListener.class.getName()).log(Level.SEVERE, null, ex);
    }
    return true;
  }
   
    public  boolean sendCountry(MyEvent evn)   {
      ServeOneThread mysrv = evn.mysrv;
    try {
      List<Country> lpt = getCountry();

      Gson gson = new Gson();
      String send = UserComman.commandGet("Country", gson.toJson(lpt));
      
      try {
        mysrv.sendUser(send);
      } catch (Exception e)
       {
         System.out.println("No Server");
       }
      
    } catch (SQLException ex) {
      Logger.getLogger(MyrealListener.class.getName()).log(Level.SEVERE, null, ex);
    }
    return true;
    }
    
     public boolean sendCity(MyEvent evn)  {
       ServeOneThread mysrv = evn.mysrv;
       String id = evn.add;
    try {
      List<City> lpt = getCityss(id);

      Gson gson = new Gson();
      String send = UserComman.commandGet("City", gson.toJson(lpt));
      try {
        mysrv.sendUser(send);
      } catch (Exception e)
       {
         System.out.println("No Server");
       }
      
    } catch (SQLException ex) {
      Logger.getLogger(MyrealListener.class.getName()).log(Level.SEVERE, null, ex);
    }
    return true;
    }
     
     public boolean sendStreet(MyEvent evn)  {
       ServeOneThread mysrv = evn.mysrv;
       String id = evn.add;
    try {
      List<Street> lpt = getStreetss(id);

    Gson gson = new Gson();
    String send = UserComman.commandGet("Street", gson.toJson(lpt));
    try {
        mysrv.sendUser(send);
    } catch (Exception e)
       {
         System.out.println("No Server");
       }
    } catch (SQLException ex) {
      Logger.getLogger(MyrealListener.class.getName()).log(Level.SEVERE, null, ex);
    }
    return true;
    }
     
    public boolean savePersone(MyEvent evn) {
      FullPersone fp = evn.fp;
      ServeOneThread mysrv = evn.mysrv;
    try {
      if (fp.ID != null)
        realSavePersone(fp);
      else
        addPersone(fp, mysrv);
    } catch (SQLException ex) {
      Logger.getLogger(MyrealListener.class.getName()).log(Level.SEVERE, null, ex);
    }
    return true;
  }
    
     public boolean saveTypePhone(MyEvent evn)  {
      TypePhone fp = evn.tp;
      ServeOneThread mysrv = evn.mysrv;
    try {
      Statement statement = Conector.conect();
      
       String query = "SELECT MAX(id) id FROM teltype";
       ResultSet resultSet = statement.executeQuery(query);
       resultSet.next();
       int id = resultSet.getInt(1);
       id = id + 1;  
       
       query = "INSERT INTO teltype (id, telcat) VALUES ('" + id + "', '" + fp.Type + "')";
       statement.executeUpdate(query);

       query = "SHUTDOWN";
       statement.execute(query);
    } catch (SQLException ex) {
      Logger.getLogger(MyrealListener.class.getName()).log(Level.SEVERE, null, ex);
    }
    return true;
  }
     
      public boolean saveTypeAdress(MyEvent evn) {
        TypeAdress fp = evn.ta;
        ServeOneThread mysrv = evn.mysrv;
    try {
      Statement statement = Conector.conect();
      
       String query = "SELECT MAX(id) id FROM adrtype";
       ResultSet resultSet = statement.executeQuery(query);
       resultSet.next();
       int id = resultSet.getInt(1);
       id = id + 1;  
       
       query = "INSERT INTO adrtype (id, adrcat) VALUES ('" + id + "', '" + fp.Type + "')";
       statement.executeUpdate(query);

       query = "SHUTDOWN";
       statement.execute(query);
    } catch (SQLException ex) {
      Logger.getLogger(MyrealListener.class.getName()).log(Level.SEVERE, null, ex);
    }
    return true;
  }
      
    public boolean saveCountry(MyEvent evn) {
      Country fp = evn.country;
      ServeOneThread mysrv = evn.mysrv;
    try {
      Statement statement = Conector.conect();

      String query = "SELECT MAX(id) id FROM country";
      ResultSet resultSet = statement.executeQuery(query);
      resultSet.next();
      int id = resultSet.getInt(1);
      id = id + 1;

      query = "INSERT INTO country (id, ncountry) VALUES ('" + id + "', '" + fp.name + "')";
      statement.executeUpdate(query);

      query = "SHUTDOWN";
      statement.execute(query);
    } catch (SQLException ex) {
      Logger.getLogger(MyrealListener.class.getName()).log(Level.SEVERE, null, ex);
    }
    return true;
  }
  
    public boolean saveCity(MyEvent evn)  {
      ServeOneThread mysrv = evn.mysrv;
      City fp = evn.city;
    try {
      Statement statement = Conector.conect();

      String query = "SELECT MAX(id) id FROM city";
      ResultSet resultSet = statement.executeQuery(query);
      resultSet.next();
      int id = resultSet.getInt(1);
      id = id + 1;

      query = "INSERT INTO city (id, ncity, countid) VALUES ('" + id + "', '" + fp.name + "', '" + fp.OID + "')";
      statement.executeUpdate(query);

      query = "SHUTDOWN";
      statement.execute(query);
      
    } catch (SQLException ex) {
      Logger.getLogger(MyrealListener.class.getName()).log(Level.SEVERE, null, ex);
    }
    return true;
  }
    
    public boolean saveStreet(MyEvent evn)  {
      ServeOneThread mysrv = evn.mysrv;
      Street fp = evn.street;
    try {
      Statement statement = Conector.conect();

      String query = "SELECT MAX(id) id FROM street";
      ResultSet resultSet = statement.executeQuery(query);
      resultSet.next();
      int id = resultSet.getInt(1);
      id = id + 1;

      query = "INSERT INTO street (id, nstreet, cityid) VALUES ('" + id + "', '" + fp.name + "', '" + fp.OID + "')";
      statement.executeUpdate(query);

      query = "SHUTDOWN";
      statement.execute(query);
      
    } catch (SQLException ex) {
      Logger.getLogger(MyrealListener.class.getName()).log(Level.SEVERE, null, ex);
    }
    return true;
  }
    
    public boolean delPersone(MyEvent evn)  {
      FullPersone fp = evn.fp;
      ServeOneThread mysrv = evn.mysrv;
    try {
      Statement statement = Conector.conect();
      String query = "DELETE FROM persone WHERE id = '" + fp.ID + "'";
      statement.executeUpdate(query);
      
      for (Phone p : fp.Phones)
      {
        query = "DELETE FROM phone WHERE id = '" + p.ID + "'";
        statement.executeUpdate(query);
      }
      
      for (Address p : fp.Addres)
      {
        query = "DELETE FROM adres WHERE id = '" + p.ID + "'";
        statement.executeUpdate(query);
      }

      query = "SHUTDOWN";
      statement.execute(query);
    } catch (SQLException ex) {
      Logger.getLogger(MyrealListener.class.getName()).log(Level.SEVERE, null, ex);
    }
    return true;
  }

    public boolean delPhone(MyEvent myEvent) {
        String add = myEvent.add;
         try {
            Statement statement = Conector.conect();
            String query = "DELETE FROM phone WHERE id = '" + add + "'";
            statement.executeUpdate(query);

            query = "SHUTDOWN";
            statement.execute(query);
        } catch (SQLException ex) {
            Logger.getLogger(MyrealListener.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }

    @Override
    public boolean delAdres(MyEvent myEvent) {
        String add = myEvent.add;
         try {
            Statement statement = Conector.conect();
            String query = "DELETE FROM adres WHERE id = '" + add + "'";
            statement.executeUpdate(query);

            query = "SHUTDOWN";
            statement.execute(query);
        } catch (SQLException ex) {
            Logger.getLogger(MyrealListener.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
    }
    
   @Override
  public boolean getFoto(MyEvent myEvent) {
        try {
            String id = myEvent.add;
            ServeOneThread mysrv = myEvent.mysrv;
              InputStream is = null;
              File file = new File("src/foto/foto-id" + id + ".jpg");
              is = new BufferedInputStream(new FileInputStream(file));

              int leng = is.available();
              int[] cbuf = new int[leng];
              int i = 0;
              for (int b = is.read(); b != -1; b = is.read()) {
                  cbuf[i] =  b;
                  i++;
              }
              Gson gson = new Gson();
              int[] values = { 1 };
              String json = gson.toJson(cbuf);
              String send = UserComman.commandGet("Foto", json);
              mysrv.sendUser(send);
              
 
              
        } catch (IOException ex) {
            Logger.getLogger(MyrealListener.class.getName()).log(Level.SEVERE, null, ex);
        }
        return true;
  }
   
   private static TypePhone getPhonesType(String id) throws SQLException
  {
      TypePhone pt = new TypePhone();
      
      Statement statement = Conector.conect();
      String query = "SELECT telcat FROM teltype WHERE id ='" + id + "'";
      ResultSet resultSet = statement.executeQuery(query);
      resultSet.next();
      
      pt.Type = resultSet.getString(1);
      
      return pt;
  }
  
  public static List<TypePhone> getPhoneT() throws SQLException {
      List<TypePhone> pt = new ArrayList<TypePhone>();
      
      Statement statement = Conector.conect();
      String query = "SELECT telcat, id FROM teltype";
      ResultSet resultSet = statement.executeQuery(query);
      while (resultSet.next())
      {
          TypePhone t = new TypePhone();
          t.Type = resultSet.getString(1);
          t.ID = resultSet.getString(2);
          pt.add(t);
      }
      return pt;
  }

  
  public static List<TypeAdress> getAdrT() throws SQLException {
      List<TypeAdress> pt = new ArrayList<TypeAdress>();
      
      Statement statement = Conector.conect();
      String query = "SELECT adrcat FROM adrtype";
      ResultSet resultSet = statement.executeQuery(query);
      while (resultSet.next())
      {
          TypeAdress t = new TypeAdress();
          t.Type = resultSet.getString(1);
          pt.add(t);
      }
      return pt;
  }
  

  
  public static List<Country> getCountry() throws SQLException {
      List<Country> pt = new ArrayList<Country>();
      
      Statement statement = Conector.conect();
      String query = "SELECT ncountry, id FROM country";
      ResultSet resultSet = statement.executeQuery(query);
      while (resultSet.next())
      {
          Country t = new Country();
          t.name = resultSet.getString(1);
          t.ID = resultSet.getString(2);
          pt.add(t);
      }
      return pt;
  }
  

  
  public static List<City> getCityss(String id) throws SQLException {
      List<City> pt = new ArrayList<City>();
      
      Statement statement = Conector.conect();
      String query;
      if (id.equals("-1"))
      {
        query = "SELECT ncity, id FROM city";
      }
      else{
        query = "SELECT ncity, id FROM city WHERE countid = '" + id + "'";
      }
        
      ResultSet resultSet = statement.executeQuery(query);
      while (resultSet.next())
      {
          City t = new City();
          t.name = resultSet.getString(1);
          t.ID = resultSet.getString(2);
          pt.add(t);
      }
      return pt;
  }
  

   
   public static List<Street> getStreetss(String id) throws SQLException {
      List<Street> pt = new ArrayList<Street>();
      
      Statement statement = Conector.conect();
      String query;
      if (id.equals("-1"))
      {
        query = "SELECT nstreet, id FROM street";
      }
      else {
        query = "SELECT nstreet, id FROM street WHERE cityid = '" + id + "'";
      }
      ResultSet resultSet = statement.executeQuery(query);
      while (resultSet.next())
      {
          Street t = new Street();
          t.name = resultSet.getString(1);
          t.ID = resultSet.getString(2);
          pt.add(t);
      }
      return pt;
  }
  
  public static List<Phone> getPhones(String id) throws SQLException
  {
      List<Phone> Phones = new ArrayList<Phone>();
      String str ="";
      
      Statement statement = Conector.conect();
      String query = "SELECT id, nphone, teltypeid FROM phone WHERE personid ='" + id + "'";
      ResultSet resultSet = statement.executeQuery(query);
      while (resultSet.next())
      {
        Phone p = new Phone();  
        p.ID = resultSet.getString(1);
        p.Number = resultSet.getString(2);
        String typeid = resultSet.getString(3);
        TypePhone pt = getPhonesType(typeid);
        p.Type = pt;
        Phones.add(p);
        //  System.out.println(p.);
      }

      return Phones;
  } 
  
  private static Country getCountry(String id) throws SQLException
  {
      Country pt = new Country();
      
      Statement statement = Conector.conect();
      String query = "SELECT ncountry FROM country WHERE id ='" + id + "'";
      ResultSet resultSet = statement.executeQuery(query);
      resultSet.next();
      
      pt.name = resultSet.getString(1);
      
      return pt;
  }
  
  private static City getCity(String id) throws SQLException
  {
      City pt = new City();
      
      Statement statement = Conector.conect();
      String query = "SELECT ncity FROM city WHERE id ='" + id + "'";
      ResultSet resultSet = statement.executeQuery(query);
      resultSet.next();
      
      pt.name = resultSet.getString(1);
      
      return pt;
  }
  
  private static Street getStreet(String id) throws SQLException
  {
      Street pt = new Street();
      
      Statement statement = Conector.conect();
      String query = "SELECT nstreet FROM street WHERE id ='" + id + "'";
      ResultSet resultSet = statement.executeQuery(query);
      resultSet.next();
      
      pt.name = resultSet.getString(1);
      
      return pt;
  }
  
  private static TypeAdress getAdressType(String id) throws SQLException
  {
      TypeAdress pt = new TypeAdress();
      
      Statement statement = Conector.conect();
      String query = "SELECT adrcat FROM adrtype WHERE id ='" + id + "'";
      ResultSet resultSet = statement.executeQuery(query);
      resultSet.next();
      
      pt.Type = resultSet.getString(1);
      
      return pt;
  }
  
  public static List<Address> getAdddress(String id) throws SQLException
  {
      List<Address> Addreses = new ArrayList<Address>();
      String str ="";
      
      Statement statement = Conector.conect();
      String query = "SELECT id, country, city, street, add, typeid FROM adres WHERE personeid ='" + id + "'";
      ResultSet resultSet = statement.executeQuery(query);
      while (resultSet.next())
      {
        Address p = new Address();  
        p.ID = resultSet.getString(1);
        p.country = getCountry(resultSet.getString(2));
        p.city = getCity(resultSet.getString(3));
        p.street = getStreet(resultSet.getString(4));
        p.Add = resultSet.getString(5);
        p.Type = getAdressType(resultSet.getString(6));
        
        Addreses.add(p);
        //  System.out.println(p.);
      }

      return Addreses;
  }
  

  
   public static boolean realSavePersone(FullPersone fp) throws SQLException {
     Statement statement = Conector.conect();
     String query = "UPDATE persone SET fname = '" + fp.FName + "', lname = '" + fp.LName + "', bd = '" + fp.bd + "', uid = '" + fp.uid + "'  WHERE id = '" + fp.ID + "'";
     statement.executeUpdate(query);
     
     savePhones(fp);
     saveAdres(fp);

     query = "SHUTDOWN";
     statement.execute(query);
     return true;
  }
   public static boolean addPersone(FullPersone fp, ServeOneThread mysrv) throws SQLException
   {
     Statement statement = Conector.conect();
     
     String query = "SELECT MAX(id) id FROM persone";
     ResultSet resultSet = statement.executeQuery(query);
     resultSet.next();
     int id = resultSet.getInt(1);
     id = id + 1;  
     fp.ID = Integer.toString(id);
     
     // нет метода добавления тел и адр
     
     query = "INSERT INTO persone (id, fname, lname, bd, uid) VALUES ('" + id + "', '" + fp.FName + "', '" + fp.LName + "', '" + fp.LName + "', '" + mysrv.p.getId() + "')";
     statement.executeUpdate(query);
     
     savePhones(fp);
     saveAdres(fp);

     query = "SHUTDOWN";
     statement.execute(query);
     return true;
   }
   
   private static boolean saveAdres(FullPersone fp) throws SQLException {
     Statement statement = Conector.conect();
     if (fp.Addres != null){
         for (Address p : fp.Addres) {
             String query = "SELECT id FROM country WHERE ncountry = '" + p.country.name + "'";
             ResultSet resultSet = statement.executeQuery(query);
             resultSet.next();
             String countryid = resultSet.getString(1);

             query = "SELECT id FROM city WHERE ncity = '" + p.city.name + "'";
             resultSet = statement.executeQuery(query);
             resultSet.next();
             String cityid = resultSet.getString(1);

             query = "SELECT id FROM street WHERE nstreet = '" + p.street.name + "'";
             resultSet = statement.executeQuery(query);
             resultSet.next();
             String streetid = resultSet.getString(1);

             query = "SELECT id FROM adrtype WHERE adrcat = '" + p.Type.Type + "'";
             resultSet = statement.executeQuery(query);
             resultSet.next();
             String typeid = resultSet.getString(1);

             query = "SELECT MAX(id) id FROM adres";
             resultSet = statement.executeQuery(query);
             resultSet.next();
             int nid = resultSet.getInt(1);
             nid++;

             if (p.ID != null) {
                 query = "UPDATE adres SET country = '" + countryid + "', city = '" + cityid + "', street = '" + streetid + "', add = '" + p.Add + "', typeid = '" + typeid + "'  WHERE id = '" + p.ID + "'";
             } else {
                 query = "INSERT INTO adres (id, country, city, street, add, typeid, personeid) VALUES ('" + nid + "', '" + countryid + "', "
                         + "'" + cityid + "', '" + streetid + "', '" + p.Add + "', '" + typeid + "', '" + fp.ID + "')";
             }

             statement.executeUpdate(query);
         }
     }
     
         
     return true;
  }
   
   
   private static boolean savePhones(FullPersone fp) throws SQLException {
     
     for (Phone p : fp.Phones)
     {
         if (p.ID != null && !p.ID.equals(""))
             savePhon(p);
         else
             addPhon(p, fp.ID);
     }
         
     return true;
  }
   
   private static boolean savePhon(Phone p) throws SQLException {
       Statement statement = Conector.conect();
       String query = "SELECT id FROM teltype WHERE telcat = '" + p.Type.Type + "'";
       ResultSet resultSet = statement.executeQuery(query);
       resultSet.next();
       String typeid = resultSet.getString(1);

       query = "UPDATE phone SET nphone = '" + p.Number + "', teltypeid = '" + typeid + "'  WHERE id = '" + p.ID + "'";
       statement.executeUpdate(query);
       return true;
   }
   
   private static boolean addPhon(Phone p, String pid) throws SQLException {
       Statement statement = Conector.conect();
       String query = "SELECT MAX(id) id FROM phone";
       ResultSet resultSet = statement.executeQuery(query);
       resultSet.next();
       int id = resultSet.getInt(1);
       id++;
       
       query = "SELECT id FROM teltype WHERE telcat = '" + p.Type.Type + "'";
       resultSet = statement.executeQuery(query);
       resultSet.next();
       String typeid = resultSet.getString(1);
       
       query = "INSERT INTO phone (id, nphone, teltypeid, personid) VALUES ('" + id + "', '" + p.Number + "', '" + typeid + "', '" + pid + "')";
       statement.executeUpdate(query);
       return true;
   }
}
